# -*- coding: utf-8 -*- 
from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User


class Member(models.Model):
    #name = models.CharField(verbose_name="imię",maxlength=20)
    #surname = models.CharField(verbose_name="nazwisko",maxlength=20,blank=True)
    user = models.ForeignKey(User,unique=True) 
    membership = models.ForeignKey("MembershipType") 
    nickname = models.CharField(verbose_name="nick",maxlength=20,blank=False)
    #email = models.EmailField(blank=True)
    jabber = models.EmailField(blank=True)
    gg = models.IntegerField(blank=True,null=True)
    skype = models.CharField(maxlength=20,blank=True)
    
    avatar = models.ImageField(upload_to="avatar",blank=True,null=True)
    foto = models.ImageField(upload_to="foto",blank=True,null=True)
    cv = models.FileField(upload_to="cv",blank=True)
    about = models.TextField(blank=True)
    interests = models.TextField(blank=True)
    
    birthday = models.DateTimeField(u'data urodzin',blank=True,null=True)
    joindate = models.DateTimeField(u'data wstąpienia do ASI',auto_now_add=True)
    verification = models.IntegerField(choices=((0,u"Nie zweryfikowany"),(1,u"Pomyślnie zweryfikowany"),(2,u"Negatywnie zweryfikowany")),default=0)
  
    #login = models.CharField(maxlength=20,unique=True)
    
    def __unicode__(self):
      return u' '.join( [self.nickname,"-",self.user.first_name,self.user.last_name] )
    class Meta:
        verbose_name = u'członek'
        verbose_name_plural = u'członkowie'
    class Admin:
        list_display = ("user","nickname","membership")
        search_fields = ['about','insterests']
    def get_absolute_url(self):
        return "/members/profile/%i/" % self.id


class MembershipType(models.Model):
    name = models.CharField(maxlength=20)
    level = models.IntegerField()
    def __unicode__(self):
        return u'%s' % self.name
    class Meta:
        verbose_name = u'typ członkowstwa'
        verbose_name_plural = u'typy członkowstwa'
    class Admin:
        list_display = ("name","level")


class Group(models.Model):
    '''
    ASI organisation model consists of several groups of specialists.
    Each group has a branch of responsibilities and projects to maintain.
    Project and group are binded by the person of project's leader if one belongs to couple groups,
    then it's maintained by all groups that 
    '''
    name = models.CharField(maxlength=20,unique=True)
    shortname = models.CharField(maxlength=20,blank=True,unique=True)
    leader = models.ForeignKey(Member)
    icon = models.ImageField(upload_to="logo",blank=True,null=True)
    about = models.TextField(verbose_name="obszar kompetencji grupy",blank=True)
    def save(self):
      super(Group,self).save()
      try:
          GroupMember.objects.get(member=self.leader,group=self.id)
      except ObjectDoesNotExist:
          GroupMember(group=self,member=self.leader,responsibilities=u"Leader tej grupy. Proszę uzupełnij swoje obowiązki.").save()
    def __unicode__(self):
        return u'%s' % self.name
    class Meta:
        verbose_name = u'grupa'
        verbose_name_plural = u'grupy'
    class Admin:
        list_display = ("shortname","leader")  
        search_fields = ['about']
    def get_absolute_url(self):
        return "/groups/%i/" % self.id

class GroupMember(models.Model):
    group = models.ForeignKey(Group)
    member = models.ForeignKey(Member)
    responsibilities = models.TextField()
    def __unicode__(self):
        return u'%s' % (self.group)
    class Meta:
        verbose_name = u'członek grupy'
        verbose_name_plural = u'członkowie grupy'
    class Admin:
        list_display = ("group","member")  

